package com.myplans.client.view.custom;

import java.util.Date;

import com.google.gwt.core.client.GWT;
import com.google.gwt.event.dom.client.ClickEvent;
import com.google.gwt.i18n.client.DateTimeFormat;
import com.google.gwt.uibinder.client.UiBinder;
import com.google.gwt.uibinder.client.UiField;
import com.google.gwt.uibinder.client.UiHandler;
import com.google.gwt.user.client.ui.Button;
import com.google.gwt.user.client.ui.CheckBox;
import com.google.gwt.user.client.ui.Composite;
import com.google.gwt.user.client.ui.DialogBox;
import com.google.gwt.user.client.ui.HasText;
import com.google.gwt.user.client.ui.ListBox;
import com.google.gwt.user.client.ui.TextArea;
import com.google.gwt.user.client.ui.TextBox;
import com.google.gwt.user.client.ui.Widget;
import com.google.gwt.user.datepicker.client.DateBox;

public class AddPlanDialogBox extends Composite implements HasText {

	private static AddPlanDialogBoxUiBinder uiBinder = GWT
			.create(AddPlanDialogBoxUiBinder.class);

	interface AddPlanDialogBoxUiBinder extends UiBinder<Widget, AddPlanDialogBox> {
	}

	public AddPlanDialogBox() {
		initWidget(uiBinder.createAndBindUi(this));
		dialogBox.hide();
		DateTimeFormat dateFormat = DateTimeFormat.getFormat("MMM d, yyyy");
		dateBox.setFormat(new DateBox.DefaultFormat(dateFormat));
		for (int i = 1; i <= 12; i++) {
			hours.addItem(String.valueOf(i));
		}
		for (int i = 0; i <= 55; i++) {
			if (i % 5 == 0) {
				if (i < 10) {
					minutes.addItem("0" + String.valueOf(i));
				} else {
					minutes.addItem(String.valueOf(i));
				}
			}
		}
		ampm.addItem("AM");
		ampm.addItem("PM");
	}

	@UiField
	Button button;
	@UiField
	DialogBox dialogBox;
	@UiField
	TextBox name;
	@UiField
	TextBox location;
	@UiField
	TextArea description;
	@UiField
	Button addPlanButton;
	@UiField
	ListBox category;
	@UiField
	DateBox dateBox;
	@UiField
	ListBox hours;
	@UiField
	ListBox minutes;
	@UiField
	ListBox ampm;
	@UiField
	CheckBox shareOnFacebook;

	public CheckBox getShareOnFacebook() {
		return shareOnFacebook;
	}

	public AddPlanDialogBox(String firstName) {
		initWidget(uiBinder.createAndBindUi(this));
		button.setText(firstName);
	}

	@UiHandler("button")
	void onClick(ClickEvent e) {
		dialogBox.hide();
	}

	public void setText(String text) {
		button.setText(text);
	}

	public String getText() {
		return button.getText();
	}
	
	public void show() {
		dialogBox.center();
	}

	public TextBox getName() {
		return name;
	}

	public TextBox getLocation() {
		return location;
	}

	public TextArea getDescription() {
		return description;
	}

	public Button getAddPlanButton() {
		return addPlanButton;
	}

	public DialogBox getDialogBox() {
		return dialogBox;
	}

	public ListBox getCategory() {
		return category;
	}
	
	public Date getDate() {
		DateTimeFormat format = DateTimeFormat.getFormat("MMM d, yyyy h m a");
		Date date = format.parse(dateBox.getTextBox().getText() + " " + hours.
				getValue(hours.getSelectedIndex()) + " " + Integer.parseInt(minutes.
				getValue(minutes.getSelectedIndex())) + " " + ampm.
				getValue(ampm.getSelectedIndex()));
		
		return date;
	}
	
}
